﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KitBuildingWF.Models;
using Advantage.Data;
using Advantage.Data.Provider;
using System.Collections;

namespace KitBuildingWF.DataAccess
{
    public class KitNativeDAO : GenericNativeDAO<Kit>, KitDAO
    {
        AdsConnection conn;
        AdsCommand cmd;
        AdsDataReader adsReader;
        int iField;
        // Map Kit class properties to Kit database table columns
        Dictionary<String, String> kitPropertiesMap = new Dictionary<string,string>();

        public KitNativeDAO() {
            kitPropertiesMap.Add("KITCODE", "KitCode");
            kitPropertiesMap.Add("KITDESCRIPTION", "KitDescription");
            kitPropertiesMap.Add("KITITEM", "contents");
            kitPropertiesMap.Add("ITEMQTY", "contents");
        }

        /// <summary>
        /// KitCode used as Kit ID
        /// </summary>
        /// <param name="kitID"></param>
        /// <returns></returns>
        public Kit findById(string kitID)
        {
            conn = GetConnection();
            conn.Open();
            cmd = conn.CreateCommand();
            Dictionary<String, Int32> kitContents = new Dictionary<string, int>();
            Kit returnKit = new Kit();
            string kitItem = null;
            Int32 itemQty = 0;

            string cmdStatement = "select * from KitsMain where KITCODE = '" + kitID + "'";
            cmd.CommandText = cmdStatement;

            adsReader = cmd.ExecuteReader();

            while (adsReader.Read())
            {
                for (iField = 0; iField < adsReader.FieldCount; iField++)
                {
                    if (adsReader.GetName(iField).Equals("KITCODE"))
                        returnKit.kitCode = adsReader.GetValue(iField).ToString();
                    

                    if (adsReader.GetName(iField).Equals("KITDESCRIPTION"))
                        returnKit.kitDescription = adsReader.GetValue(iField).ToString();

                    if (adsReader.GetName(iField).Equals("KITITEM"))
                        kitItem = adsReader.GetValue(iField).ToString();

                    if (adsReader.GetName(iField).Equals("ITEMQTY"))
                        itemQty = Convert.ToInt32(adsReader.GetValue(iField));

                    if (adsReader.GetName(iField).Equals("CAPITALCODE"))
                        returnKit.capitalCode = adsReader.GetValue(iField).ToString();
                }

                // TODO
                ////return Kit.contents.Add(kitItem, itemQty);

            }

            return returnKit;
        }

        public List<Kit> findAll()
        {
            Stack<Kit> kitStack = new Stack<Kit>();
            List<Kit> kitList = new List<Kit>();
            conn = GetConnection();
            conn.Open();
            cmd = conn.CreateCommand();
            Dictionary<String, Int32> kitContents = new Dictionary<string, int>();
            Kit kit = new Kit();
            string kitItem = null;
            Int32 itemQty = 0;
            string curKitCode = null;
            string curkitDescription = null;
            string curCapitalCode = null;

            // sort the search result by kitcode, so any kit line would be together. 
            string cmdStatement = "select * from KitMain order by KITCODE";
            cmd.CommandText = cmdStatement;

            adsReader = cmd.ExecuteReader();


            while (adsReader.Read())
            {
                for (iField = 0; iField < adsReader.FieldCount; iField++)
                {
                    if (adsReader.GetName(iField).Equals("KITCODE") && (adsReader.GetValue(iField).ToString() != curKitCode))
                    {
                        // Create a new Kit when the kit code doesn't match previously processed kit
                        kitStack.Push(new Kit());
                        curKitCode = adsReader.GetValue(iField).ToString();
                    }

                    if (adsReader.GetName(iField).Equals("KITDESCRIPTION"))
                        curkitDescription = adsReader.GetValue(iField).ToString();

                    if (adsReader.GetName(iField).Equals("KITITEM"))
                        kitItem = adsReader.GetValue(iField).ToString();

                    if (adsReader.GetName(iField).Equals("ITEMQTY"))
                        itemQty = Convert.ToInt32(adsReader.GetValue(iField));

                    if (adsReader.GetName(iField).Equals("CAPITALCODE"))
                        curCapitalCode = adsReader.GetValue(iField).ToString();
                }

                // update or add new content to current kit
                if (kitStack.Count > 0)
                {
                    kitStack.Peek().kitCode = curKitCode;
                    kitStack.Peek().kitDescription = curkitDescription;
                    kitStack.Peek().capitalCode = curCapitalCode;
                    // TODO
                    ////kitStack.Peek().contents.Add(kitItem, itemQty);
                }

            }
            
            // copy all the item from the stack to list for returning.
            while (kitStack.Count > 0)
            {
                kitList.Add(kitStack.Pop());
            }

            return kitList;
        }
    }
}
